package com.itianyu.blog.web.controller;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.itianyu.blog.util.QiniuUtil;
import com.qiniu.util.Json;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import java.io.*;
import java.text.SimpleDateFormat;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.json.simple.*;

@Controller
@RequestMapping("/common")
public class CommonController {

	@ResponseBody
	@RequestMapping(value = "/getToken")
	public String getToken() throws Exception {
		String token = QiniuUtil.getQiuNiuToken();
		// Gson gson = new Gson();
		// String result = gson.toJson(token);
		String json = "{'message':'" + token + "'}";
		System.out.println(json);
		return token;

	}

	/* 测试文件上传 */
	@RequestMapping("/uploads")
	public @ResponseBody String uploads(HttpServletRequest request, MultipartFile filename) throws Exception {
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/blog"+"/";
		if (filename != null && filename.getOriginalFilename() != null && filename.getOriginalFilename().length() > 0) {
			String path = request.getSession().getServletContext().getRealPath("/");
			Calendar cal = Calendar.getInstance();
			int month = cal.get(Calendar.MONTH) + 1;
			int year = cal.get(Calendar.YEAR);
			String filePath = path + year + month + "/";
			File dir = new File(filePath);
			if (!dir.isDirectory())
				dir.mkdir();

			String fileOriginalName = filename.getOriginalFilename();
			String newFileName = UUID.randomUUID() + fileOriginalName.substring(fileOriginalName.lastIndexOf("."));
			File file = new File(filePath + newFileName);
			// 文件写入磁盘
			filename.transferTo(file);
			// 返回存储的相对路径+文件名称

			List<String> list = new ArrayList<String>();
			list.add(basePath + year + month + "/" + newFileName);
			Gson gson = new Gson();
	  	  String json=gson.toJson(list);
			// list转成json
	  	  
	      Map map=new HashMap();
	      map.put("errno", "0");
	      map.put("data", list);
	  	  
	      String jsonStr = gson.toJson(map);

			return jsonStr;
		}

		String url = null;
		url = "{\r\n" + "    \"errno\": \"0\",\r\n" + "    \"data\": [\r\n"
				+ "        \"http://www.ewhale.cn/wp-content/uploads/2015/07/about-1.jpg\",\r\n"
				+ "        \"http://www.ewhale.cn/wp-content/uploads/2015/07/about-1.jpg\"\r\n" + "    ]\r\n" + "}";
		return url;
	}

	public static void main(String args[]) {

		List<String> list = new ArrayList<String>();
		list.add("abc");
		list.add("123");

		// list转成json
		String json = JSONArray.toJSONString(list);

		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("errno", "0");
		jsonObject.addProperty("data", json);
		System.out.println(jsonObject);

	}

}
